package mapViewer.screens;



//import util.Base64;
import java.util.Vector;

import javax.microedition.lcdui.*;

import context.*;

import mapViewer.AccountRecord;
import mapViewer.AccountRecordStore;
import mapViewer.MapViewer;

// ******************************************************************
// *** CLASS: AccountScreen
// ******************************************************************
public class AccountScreen extends Form implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
  private static final Command okCommand=new Command("OK",Command.EXIT,0);
  private static final Command editCommand=new Command("Edit",Command.ITEM,1);
  private static final Command newCommand=new Command("New",Command.ITEM,2);
  private static final Command deleteCommand=new Command("Delete",Command.ITEM,3);
  private static final Command cancelCommand=new Command("Cancel",Command.ITEM,4);

  private MapViewer midlet;
  private Display display;
  private Displayable prev;
  private ChoiceGroup accountChooser;


  public AccountScreen(final MapViewer midlet)
  { super("Account:");
    this.midlet=midlet;
    this.display=Display.getDisplay(midlet);
    if(DEBUG) Debug.println("New AccountScreen");
    this.addCommand(okCommand);
    appendAccountChooser();    
    this.setCommandListener(new CommandListener()
    { public void commandAction(Command c,Displayable s)
      { if(DEBUG) Debug.println("AccountScreen'CommandListener.commandAction:"+c);
    	if(c==cancelCommand) mainCancelAction();
        else if(c==okCommand) mainOKAction();
      }
    });
  }
  
  private void appendAccountChooser()
  { AccountRecord selectedAccount=AccountRecordStore.getSelectedAccount();
	Vector accounts=AccountRecordStore.getAccounts();
	int nAccount=accounts.size();
	String[] nameList=new String[nAccount];
	int selectedAccountIndex=0;
	for(int i=0;i<nAccount;i++)
	{ AccountRecord rec=(AccountRecord)accounts.elementAt(i);
	  if(rec==selectedAccount) selectedAccountIndex=i;
	  nameList[i]=rec.getAccountName();
	}
	accountChooser=new ChoiceGroup("Accounts",Choice.EXCLUSIVE,nameList,null);
	accountChooser.setSelectedIndex(selectedAccountIndex,true);
	    
	append(accountChooser);
	accountChooser.addCommand(editCommand);
	accountChooser.addCommand(newCommand);
	accountChooser.addCommand(deleteCommand);
	accountChooser.setItemCommandListener(new ItemCommandListener()
	{ public void commandAction(Command c,Item item)
	  { if(DEBUG) Debug.println("AccountScreen'ItemCommandListener(accountChooser).commandAction:"+c+", item="+item);
	    if(c==editCommand) editAccountCommand(item);
	    else if(c==newCommand) newAccountCommand(item);
	    else if(c==deleteCommand) deleteAccountCommand(item);
	  }
	});  
  }  
  
  private void mainCancelAction() { display.setCurrent(prev); }

  private void mainOKAction()
  { // update accountRecordStore
    String selectedAccount=accountChooser.getString(accountChooser.getSelectedIndex());
    if(DEBUG) Debug.println("AccountScreen.okAction: Selected AccountName: "+selectedAccount);
    try { AccountRecordStore.setSelectedAccount(selectedAccount); }
    catch(Exception e) { Debug.EXCEPTION("AccountScreen.okAction: ",e); }
    display.setCurrent(prev);
  }
  
  public void show()
  { prev=display.getCurrent();
    display.setCurrent(this);
  }
  
  public void update()
  { deleteAll();
	appendAccountChooser();
  }
  
  private void editAccountCommand(Item item)
  { String accountName=accountChooser.getString(accountChooser.getSelectedIndex());
    if(DEBUG) Debug.println("AccountScreen.editAccountCommand: Selected AccountName: "+accountName);
	AccountRecord account=AccountRecordStore.getAccount(accountName);
    if(DEBUG) Debug.println("AccountScreen.editAccountCommand: Selected: "+account);
    if(account!=null) new AccountDetailsScreen(midlet,account).show();
  }
  
  private void newAccountCommand(Item item)
  {	AccountRecord account=AccountRecordStore.newAccount();
    if(DEBUG) Debug.println("AccountScreen.newAccountCommand: Selected: "+account);
    accountChooser.append(account.getAccountName(),null);
    new AccountDetailsScreen(midlet,account).show();
  }
  
  private void deleteAccountCommand(Item item)
  { String accountName=accountChooser.getString(accountChooser.getSelectedIndex());
    if(DEBUG) Debug.println("AccountScreen.deleteAccountCommand: Selected AccountName: "+accountName);
	AccountRecordStore.removeAccount(accountName);
	update();
  }
  
}
